import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk



def init_dialog(root, _options):
    def submit():
        user_input = entry.get()
        print(f"输入的用户信息是：{user_input}")
        _options.submitByDialog({"data": user_input, "success": True})
        # messagebox.showinfo("提交信息", f"你输入的内容是: {user_input}")
        popup1.destroy()

    def cancel():
        _options.cancel({"success": False})
        popup1.destroy()

    popup1 = tk.Toplevel(root)
    popup1.title(_options.title if _options.title else "提示")
    label = tk.Label(popup1, text="请输入标签:")
    label.pack()

    entry = tk.Entry(popup1)
    entry.pack()

    button_frame = tk.Frame(popup1)
    button_frame.pack()

    try:
        # 打开图片文件
        # E:\wiki\wikiq\tai\_files\1733405444.
        image = Image.open(_options.imageFilePath)
        # 将图片转换为适合tkinter使用的PhotoImage对象
        photo = ImageTk.PhotoImage(image)

        label = tk.Label(popup1, image=photo)
        label.image = photo  # 防止图片被垃圾回收
        label.pack()

    except Exception as e:
        print(f"加载图片出现错误: {e}")
        tk.messagebox.showerror("错误", f"加载图片出现错误: {e}")

    submit_button = tk.Button(button_frame, text="确认", command=submit)
    submit_button.pack(side=tk.LEFT)

    cancel_button = tk.Button(button_frame, text="取消", command=cancel)
    cancel_button.pack(side=tk.LEFT)


# if __name__ == '__main__':
#     root = tk.Tk()
#     root.withdraw()  # 隐藏主窗口
#     init_dialog(root)
#     init_dialog(root)
#     root.mainloop()
